/**
 * Copyright (c) 2016-2019 ikas All rights reserved.
 * <p>
 * https://www.ikas.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.ikas.ai.server.module;

import com.ikas.ai.server.config.EnvConfig;
import com.ikas.ai.server.module.system.model.Log;
import com.ikas.ai.server.module.system.model.LogOpsType;
import com.ikas.ai.server.module.system.model.User;
import com.ikas.ai.server.module.system.service.LogService;
import com.ikas.ai.server.module.system.service.UserService;
import com.ikas.ai.server.security.UserContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;


/**
 * Controller公共组件
 */
@SuppressWarnings("all")
public abstract class AbstractBean {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private UserService userService;
	@Autowired
	private LogService logService;
	@Autowired
	private HttpServletRequest request;

	/**
	 * 获取用户信息
	 *
	 * @return
	 */
	protected User getUser() {
		String userName = getUserName();
		if (StringUtils.isBlank(userName)) {
			return null;
		}
		return userService.getUser(userName);
	}

	/**
	 * 获取用户id主键
	 *
	 * @return
	 */
	protected Long getUserId() {
		User loginUser = getUser();
		if (null == loginUser) {
			return null;
		}
		return loginUser.getId();
	}

	/**
	 * 获取登录用户名
	 *
	 * @return
	 */
	protected String getUserName() {
		return UserContextHolder.getTokenUser().getAccount();
	}
	/**
	 * 记录操作日志
	 * @param request 请求
	 * @param type 日志类型
	 * @param msg 日志类容
	 */
	public void logOperation(HttpServletRequest request, String userName, LogOpsType type, String msg) {
		Log log = new Log();
		log.setUserName(userName);
		log.setRealUser(userName);
		log.setKind("ops");
		log.setType(type.getIndex());
		log.setMicroServer(EnvConfig.getAppSeverName());
		String ipBrowser = getIpAddress(request);
		log.setIpBrowser(ipBrowser);
		log.setLevel("1");
		log.setMsg(msg);
		// 调用方法，写入数据库
		logService.addLog(log);
	}

	/**
	 * 记录操作日志
	 * @param request 请求
	 * @param type 日志类型
	 * @param msg 日志类容
	 */
	public void logOperation(HttpServletRequest request, LogOpsType type, String msg) {
		Log log = new Log();
		String userName = getUserName();
		log.setUserName(userName);
		log.setRealUser(userName);
		log.setKind("ops");
		log.setType(type.getIndex());
		log.setMicroServer(EnvConfig.getAppSeverName());
		String ipBrowser = getIpAddress(request);
		log.setIpBrowser(ipBrowser);
		log.setLevel("1");
		log.setMsg(msg);
		// 调用方法，写入数据库
		logService.addLog(log);
	}

	private String getIpAddress(HttpServletRequest request) {
		String ip = null;
		String unKnow = "unknown";
		String ipAddresses = request.getHeader("X-Forwarded-For");
		if (ipAddresses == null || ipAddresses.length() == 0 || unKnow.equalsIgnoreCase(ipAddresses)) {
			ipAddresses = request.getHeader("Proxy-Client-IP");
		}
		if (ipAddresses == null || ipAddresses.length() == 0 || unKnow.equalsIgnoreCase(ipAddresses)) {
			ipAddresses = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ipAddresses == null || ipAddresses.length() == 0 || unKnow.equalsIgnoreCase(ipAddresses)) {
			ipAddresses = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ipAddresses == null || ipAddresses.length() == 0 || unKnow.equalsIgnoreCase(ipAddresses)) {
			ipAddresses = request.getHeader("X-Real-IP");
		}
		if (ipAddresses != null && ipAddresses.length() != 0) {
			ip = ipAddresses.split(",")[0];
		}
		if (ip == null || ip.length() == 0 || unKnow.equalsIgnoreCase(ipAddresses)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
